package com.ruoyi.web.controller.teaching;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.teaching.domain.TeachingCourse;
import com.ruoyi.teaching.service.ITeachingCourseService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 教务课程Controller
 *
 * @author william
 * @date 2022-10-10
 */
@Controller
@RequestMapping("/teaching/course")
public class TeachingCourseController extends BaseController {
    private String prefix = "teaching/course";

    @Autowired
    private ITeachingCourseService teachingCourseService;

    @RequiresPermissions("teaching:course:view")
    @GetMapping()
    public String course() {
        return prefix + "/course";
    }

    /**
     * 查询教务课程列表
     */
//    @RequiresPermissions("teaching:course:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TeachingCourse teachingCourse) {
        startPage();
        List<TeachingCourse> list = teachingCourseService.selectTeachingCourseList(teachingCourse);
        return getDataTable(list);
    }

    /**
     * 导出教务课程列表
     */
    @RequiresPermissions("teaching:course:export")
    @Log(title = "教务课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TeachingCourse teachingCourse) {
        List<TeachingCourse> list = teachingCourseService.selectTeachingCourseList(teachingCourse);
        ExcelUtil<TeachingCourse> util = new ExcelUtil<TeachingCourse>(TeachingCourse.class);
        return util.exportExcel(list, "教务课程数据");
    }

    /**
     * 新增教务课程
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存教务课程
     */
    @RequiresPermissions("teaching:course:add")
    @Log(title = "教务课程", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TeachingCourse teachingCourse) {
        teachingCourse.setCreateId(getUserId());
        teachingCourse.setCreateBy(getLoginName());
        teachingCourse.setCreateTime(new Date());
        return toAjax(teachingCourseService.insertTeachingCourse(teachingCourse));
    }

    /**
     * 修改教务课程
     */
    @RequiresPermissions("teaching:course:edit")
    @GetMapping("/edit/{courseId}")
    public String edit(@PathVariable("courseId") Long courseId, ModelMap mmap) {
        TeachingCourse teachingCourse = teachingCourseService.selectTeachingCourseByCourseId(courseId);
        mmap.put("teachingCourse", teachingCourse);
        return prefix + "/edit";
    }

    /**
     * 修改保存教务课程
     */
    @RequiresPermissions("teaching:course:edit")
    @Log(title = "教务课程", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TeachingCourse teachingCourse) {
        teachingCourse.setUpdateBy(getLoginName());
        teachingCourse.setUpdateTime(new Date());
        return toAjax(teachingCourseService.updateTeachingCourse(teachingCourse));
    }

    /**
     * 删除教务课程
     */
    @RequiresPermissions("teaching:course:remove")
    @Log(title = "教务课程", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(teachingCourseService.deleteTeachingCourseByCourseIds(ids));
    }

    @GetMapping("/detail/{courseId}")
    public String detail(@PathVariable("courseId") Long courseId, ModelMap mmap) {
        TeachingCourse teachingCourse = teachingCourseService.selectTeachingCourseByCourseId(courseId);
        mmap.put("course", teachingCourse);
        return prefix + "/detail";
    }

    /**
     * 三级联动查询课程列表
     */
    @GetMapping("/select")
    @ResponseBody
    public List<TeachingCourse> select(){
        List<TeachingCourse> list1 = teachingCourseService.selectTeachingCourseList(new TeachingCourse());
        return list1;
    }

}
